package com.zzyl.framework.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@Component
public class AiInvoker {
    @Value("${qianfan.model}")
    private String model;
    @Value("${qianfan.temperature}")
    private String temperature;

    public  String invoker(String text){
        String ak = System.getenv("QIANFAN_ACCESS_KEY");
        String sk = System.getenv("QIANFAN_SECRET_KEY");
        Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH, ak, sk);

        // 指定模型
        ChatResponse resp = qianfan.chatCompletion()
                .model(model)
                .addMessage("user", text)
                .temperature(Double.valueOf(temperature)) // 采样参数，取值范围(0,1]
                .maxOutputTokens(2000) // 模型输出最大长度，取值范围[2, 2048]
                .responseFormat("json_object")  // 模型输出格式，取值范围：text（文本）、json_object（JSON对象）
                .execute();
        return resp.getResult();
    }
}
